package app

import (
	"fmt"

	_ "github.com/go-sql-driver/mysql"
	"xorm.io/xorm"
)

var engine *xorm.Engine

func GetDbEngine() *xorm.Engine {
	config := Config()
	dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8",
		config.MySQL.User,
		config.MySQL.Password,
		config.MySQL.Host,
		config.MySQL.Port,
		config.MySQL.Database,
	)

	var err error
	engine, err = xorm.NewEngine("mysql", dataSourceName)

	if err != nil {
		panic("MySQL 连接失败：" + err.Error())
	}

	return engine
}
